/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller.Index;

import Models.Account;
import Models.Address;
import Models.DAOs.Account_DAO;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.tanesha.recaptcha.ReCaptchaImpl;
import net.tanesha.recaptcha.ReCaptchaResponse;

/**
 *
 * @author 0905
 */
public class register_action extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            
            Enumeration enumerateParameters = request.getParameterNames();             
            int paramCount = 0;
            while(enumerateParameters.hasMoreElements())
            {
                enumerateParameters.nextElement();
                paramCount++;
            }           
            if(paramCount == 0){        
                
                response.sendRedirect("register");
            }
            else
            {
                String remoteAddr = request.getRemoteAddr();
                ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
                reCaptcha.setPrivateKey("6LfdCc4SAAAAAI4p64kJ91fJl4NHEFMULd1ZP4QS");

                String challenge = request.getParameter("recaptcha_challenge_field");
                String uresponse = request.getParameter("recaptcha_response_field");
                ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(remoteAddr, challenge, uresponse);

                if(reCaptchaResponse.isValid()){
                    
                    Account newAccount = new Account();
                    Address newBillAddress = new Address();
                    Address newDeliAddress = new Address();
                    newAccount.setUsername(request.getParameter("username"));
                    newAccount.setPassword(request.getParameter("password"));
                    newAccount.setEmail(request.getParameter("email"));
                    newAccount.setFirstName(request.getParameter("firstName"));
                    newAccount.setMidName(request.getParameter("midName"));
                    newAccount.setLastName(request.getParameter("lastName"));
                    newAccount.setCardName(request.getParameter("cardName"));
                    newAccount.setCardNum(request.getParameter("cardNumber"));
                    newAccount.setCardType(request.getParameter("cardType"));
                    newAccount.setCardExp(request.getParameter("cardExp"));
                    newAccount.setUserType("0");
                    
/*                    newBillAddress.setHouseNo(request.getParameter("bHouseno"));
                    newBillAddress.setStreet(request.getParameter("bStreet"));
                    newBillAddress.setSubdivision(request.getParameter("bSubd"));
                    newBillAddress.setPostalCode(request.getParameter("bPostal"));
                    newBillAddress.setCountry(request.getParameter("bCountry"));
                    
                    newDeliAddress.setHouseNo(request.getParameter("dHouseno"));
                    newDeliAddress.setStreet(request.getParameter("dStreet"));
                    newDeliAddress.setSubdivision(request.getParameter("dSubd"));
                    newDeliAddress.setPostalCode(request.getParameter("dPostal"));
                    newDeliAddress.setCountry(request.getParameter("dCountry"));*/
    
                    boolean isAdded = new Account_DAO().addAccount(newAccount,request.getParameter("repassword"));//,newBillAddress,newDeliAddress);
                    if(isAdded)
                    {
                        String confirmMsg = "You have successfully registered.";
                        request.setAttribute("captchaMsg", confirmMsg);
                        RequestDispatcher rd = request.getRequestDispatcher("register");
                        rd.forward(request, response);
                        
                    }
                    else{
                        String confirmMsg = "Failed to register your account.";
                        request.setAttribute("captchaMsg", confirmMsg);
                        RequestDispatcher rd = request.getRequestDispatcher("register");
                        rd.forward(request, response);
                    }
                }
                else{
                    String captchaMsg = "Invalid Captcha Code";
                    request.setAttribute("captchaMsg", captchaMsg);
                    RequestDispatcher rd = request.getRequestDispatcher("register");
                    rd.forward(request, response);
                }
            }
          
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
